package com.coldchain.order.api;

import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.vo.BizDispatchingVo;
import com.coldchain.order.entity.OrderSearch;
import com.coldchain.order.service.IBizDispatchingGoodsService;
import com.coldchain.order.service.IBizDispatchingOrderService;
import com.coldchain.order.service.IBizdispatchingPersonService;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配送中心
 */
@RestController
@RequestMapping("/order/dispatching")
@CheckUserToken
public class DispatchingController  {
    private static final Logger log= LoggerFactory.getLogger(DispatchingController.class);

    @Autowired
    private IBizDispatchingGoodsService dispatchingGoodsService;

    @Autowired
    private IBizdispatchingPersonService bizdispatchingPersonService;

    @Autowired
    private IBizDispatchingOrderService bizDispatchingOrderService;

    /**
     * 获取下一个配送点
     * @param orderId
     * @return
     */
    @GetMapping("/getNextTask")
    public Object getNextTask(String orderId){
        return bizDispatchingOrderService.getNextTask(orderId);
    }

    @GetMapping("/list")
    public Object selectOrderByDriverForPage(OrderSearch search){
        search.setCysid(BaseContextHandler.getCysID());
        search.setDriverId(BaseContextHandler.getUserID());
        PageResponse<BizDispatchingVo> pageResponse=bizDispatchingOrderService.getSimpleOrderPage(search);
        return new ObjectRestResponse<>().data(pageResponse.getRecords()).succeed("ok");
    }

    /**
     * 获取一步节点
     * @param orderId
     * @param sort
     * @param receipts
     * @param accessorys
     * @return
     */
    @PostMapping("/doNext")
    public Object doNext(String orderId,Integer sort,String receipts,String accessorys){
        try {
            return bizDispatchingOrderService.doNext(BaseContextHandler.getUserID(),orderId,sort,receipts,accessorys);
        }catch (Exception e){
            return new ObjectRestResponse<>().error(e.getMessage());
        }

    }

    /**
     * 上报异常
     * @param orderId
     * @param currentNode
     * @param receipts
     * @param errorCode
     * @param errorMsg
     * @return
     */
    @PostMapping("/reportError")
    public Object reportError(String orderId,Integer currentIndex,String receipts,Integer errorCode,String errorMsg){
        try {
            return bizDispatchingOrderService.reportError(BaseContextHandler.getUserID(),orderId,currentIndex,receipts,errorCode,errorMsg);
        } catch (Exception e) {
            return new ObjectRestResponse<>().error(e.getMessage());
        }
    }
}
